package cn.wolfcode.luowowo.acticle.service.impl;


import cn.wolfcode.luowowo.acticle.domain.QuestionAndAnswerComment;
import cn.wolfcode.luowowo.acticle.mapper.QuestionAndAnswerCommentMapper;
import cn.wolfcode.luowowo.acticle.mapper.QuestionAndAnswerMapper;
import cn.wolfcode.luowowo.acticle.query.QuestionAndAnswerCommentQuery;
import cn.wolfcode.luowowo.acticle.service.IQuestionAndAnswerCommentService;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

//@Component
@Service
@Transactional
public class QuestionAndAnswerCommentServiceImpl implements IQuestionAndAnswerCommentService {
    @Autowired
    private QuestionAndAnswerCommentMapper questionAndAnswerCommentMapper;
    @Autowired
    private QuestionAndAnswerMapper questionAndAnswerMapper;
    @Reference
    private IUserInfoService userInfoService;



    @Override
    public PageInfo query(QuestionAndAnswerCommentQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());

        return new PageInfo(questionAndAnswerCommentMapper.selectForList(qo));
    }


    @Override
    public QuestionAndAnswerComment get(Long id) {
        return questionAndAnswerCommentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void save(QuestionAndAnswerComment questionAndAnswerComment,Long toId,Long replyId) {
        if (questionAndAnswerComment.getId()==null){
            questionAndAnswerComment.setReplyTime(new Date());
            questionAndAnswerComment.setToUser(userInfoService.get(toId));
            questionAndAnswerComment.setReplyId(replyId);
            questionAndAnswerMapper.updateByPrimaryKey(questionAndAnswerComment.getQuestionAndAnswer());
        }
            questionAndAnswerCommentMapper.insert(questionAndAnswerComment);
    }

    @Override
    public List<QuestionAndAnswerComment> list() {
        return questionAndAnswerCommentMapper.selectAll();
    }

    public List<QuestionAndAnswerComment> queryFirstLevelList(Long id) {
        QuestionAndAnswerCommentQuery qo = new QuestionAndAnswerCommentQuery();
        qo.setQuestionAndAnswerId(id);
        return questionAndAnswerCommentMapper.selectForList(qo);
    }


}
